Refactor with Mary, add docstrings, and better function naming