After merge and some bug fixes

Merged with Jesun's code, changed some things to better match the
instructions.