added the 1 to h
modified the double foreach