Refactored javascript to remove stuff from global scope