Move javascript to separate file and refactor