main loop is not tail recursive