add locking and keepalive thread