Revised old code, put into functions and wrote new unit tests for them