Fix bugs in scanner