Whitespace improvements for readability; conform to jshint.
Use do-while here instead of duplicating code.