working on partial rewrite an cleanup for better flask code