refactoring code for CS