fixed minor bugs and added some comments