Various refactoring  and reformatting changes.