CS fixes and a bit of refactoring to improve readability

This could be imported at the start of the file?