Refactored js function