node.js implementation working identically to python's