Added lots of comments, fixed a leak or two, did slight refactoring