JS code optimization

Optimized some of the JS code and added some comments for player
movement, which will be redone in a later update to use less resources.