CS fixes and a bit of refactoring to improve readability