Added handshake and some bug fixes