some cleanup and clearer ordering of code