basic refactoring to implement optimization