Added comments and fixed a few bugs.