invert logic to have less nesting

Exit from the function as soon as is not possible to move forward.