Bug2152 prime

A rebased version of Bug2152.  Ready for (as yet unmade) changes requested from code review.