Simplify logic and clean up index.js.