better logic for keepalive