Some refactors and formatting pass