rebased the patch for bug 877262. completed the initial version of bug 793996