rewriting to make more scalable