Cleaned up the code and added documentation.

Renamed certain variables and functions in order to make
their use more clear. Added comments explaining the functionality
of certain functions (lol).