I believe I have fixed this bug, but I'm not sure if I put the test case in the right place within the file. Please see the attached patch.