Refactoring and slight style tweaks