Moved the processing of a single key-value pair to a private method, and added a check for the last incomplete pair. The ending final key was often not recognised before