Added comments and made some bug fixes.