refactored function