refactoring - move code from client to lib