Refactored the code to work in node.js