lots of CS changes and refactorings