Finished write-up. A few bug fix and style fixes.