Some refactoring and style changes