Plenty of duplicated code removed (>50 lines)

The same if condition repeated about 10 times.