Refactoring and code style fixes