Handshake error codes and refactoring