Fix an if statement getting moved into a loop it shouldn't have