Move more logic into tested lib/