refactoring code for cleanup