Added comments and fixed various bugs.