Code reformatting, some refactoring