refactoring to be more idiomatic python, adding crude tests