refactored function and fixed typo