Refactor of the JS function