Update index.js

Refactor and reformat code