refactoring function