I rebased this patch. I also changed some variables into `bool'.