rebased patch for bug 877262
more work for bug 760876. ready for review