re-structured the way the functions pass values, hopefully made it easier for testing