More tests, fixes, refactoring, etc.