ported to node.js. still need to port logic