Added some logic to handle protocol errors. Not finished