Fixed an indexing issue, and a few minor issues pointed out during code review.