Added some testing, re-order one of the functions to be more modular.