moving code here to be shared between client and server