Make code look cleaner.

Add comments and whitespace. Change tabbing on some lines of code, and perform
better error checking in some methods.