Generalize function
Minor refactoring