added some more unit tests and comments explaining the parts of the function that had been confusing to me