Can you break up this function? It's very long and would be easy to split in at least a toserver and a toclient version