updated indexed for loops to for-each.
a few comment updates.
added a couple of error checks.
fixed typo in comment.