Adding regression tests for the behavior reported in Bug#4035, and tweaking
the code to handle these cases.