reorganize code from https://github.com/sagemathinc/cocalc/pull/3581
reorganize code from https://github.com/sagemathinc/cocalc/pull/3581

tscholl2/smc,DrXyzzy/smc,DrXyzzy/smc,tscholl2/smc,tscholl2/smc,DrXyzzy/smc,tscholl2/smc,tscholl2/smc,sagemathinc/smc,sagemathinc/smc,sagemathinc/smc,sagemathinc/smc,DrXyzzy/smc