Add comments and fixed minor bugs