Added more comments and corrected some bugs