Refactor and merge client code