Refactoring and style tweaks