Added documentation to all subroutines
Renamed/refactored a few functions to clarify meaning