refactoring and style fixes