Reverting #1143. This has too many changes in one commit.  I will add some comments in #1143