use early termination instead of deeply-nested if-s