refactoring code to more generalized