Fix and refactor broken tests