Moved certain javascript function to plugin.js and simplified the code in main.js