I think the whole code here is not necessary, see my comment some lines above.