Separate optimization from statistic redux

This is a rebased version of #1914.  I decided to go ahead and make a new PR so that it can easily be run through Travis and the like.  Otherwise no major differences.  Once the CI gives us a green checkmark this should be merged instead of #1914.