Add more comments and improve some functions.