localize definitions
breaking up the giant function
actually .. just .. why even bother with synchronous queries anymore.