Nested if to switch to idle state when not moving