First version of new code

This is ready to go to master!