Initial rewrite in Python, yet to be completed