Moved some codes from js to plugin js