Use readline instead of gets, test for EOFError
Refactor parsing to use stream reader
Tidy up of debug statements and classes