Python implementation, needs testing, docstrings, clean up