Added more comments, fixed some bug