refactor handshake