Remove extraneous debug message.
Fix some bugs.
Add more comments.
Factor out some stuff.