This implementation cries out for using async or promises or *something* to disentangle the nesting here.