Continued refactor. Updated almost all functions and documented more.