refactor out data sending code