optimize and fix failing tests