Added some tests and did some refactoring for bug #7337.