Fixed if statement referencing old variable