r1171 + merged second patch by Aaron