Refactored the functions in cpsc5210.rioux