slightly modified logic to get rid of continue statement