Removed duplicated code via looping