Improved quality matching

fixes #3829