moving the logic for inside the if condition