Rewrite index.js and add new tests