Bug fix: checking ref() instead of defined()