Merged patch from Martin