Please take a look at these improvements ;)

Needs testing, but I think it is on the right track...

Allows for much easier configuration and customisation instead of the old hard-coded approach.