Added some documentation and refactored the layout of some functions.