refactors a bit more, still fails 2 tests