move keep alive outside of if