Re-arrage a few functions, formatted code, and documented.