fixed several bugs in scanning