Optimized main.js, added comments

reduced code needed in loop, added comments