First fully functional version of the scanner. Tidied up a lot of existing code.