Several refactorings and bug fixes. Only 4 failing tests left.
Bug fixes. 1 failing test left