fix misplaced if statement bug