Parsing code tidied up a lot but not complete/tested yet