Also now has docstrings and types, and generally passes flake8 and also mypy, with the exception of that 'this' hack not being liked.