in reply to How to reduce this lengthy code?

Start by making it readable. Add white space and comments.