this block of code is repeated many times, can you rewrite it so be more modular?