Some refactoring and rearranging.