#23 split logic into client and server parts
#23 merge with formatting improvements