Fixing reference returning bugs