Patches from Marcus applied; test case from Marcus also.