Straightened up code a bit and added some notes.