re-ordered the functions for ease of reading. Added final function.
Final code commit.