Improved the scan method and fixed a LOT of bugs