Fix while loop breaking (#3472)
Cleanup, signedness, range checking (#3472)
Implement Cody's suggestions (#3472)