compacting code

actually UKHeliBob,
if you look closely at the op's posted code, he/she has 4 distinct pattern thus the many for loop