more implementation of the serial protocol. some code tidying/refactoring