Refactored javascript to .js files. Added comments where needed