Update main.js

Updated plug structure to fix scope issues. Some minor refactoring as well to increase performance.
Merge pull request #2 from lupos/master

Update main.js