Some refactoring and code style improvements.