Fixed minor bugs and added some comments