Some refactoring and code restyling