Cleanup: all TODOs fixed, some tests introduced.