fixup! fix tests and refactor some parts of this PR