Fix and optimize index.js

I haven't tested this since I am unfamiliar with `component`, but I have an adapted version of this code working elsewhere.