Move more logic into client-lib.c