reducing down the code by looping it instead