Fixing reference before assignment error