refactor and fix broken tests