Overcommented to make sure I really understand what is happening in the functions.