minor changes

more detailed if branching and better loop condition