Clean up the codes and added more comments