Slight refactor; changed keepalive function