Tidied code, improved duplicate matching etc.