reindent, and some refactoring/rearranging