Cleaning up some codes and adding comments