refactored functions in js