The code that have posted is rather cumbersome, but it does have one significant advantage: it will be much easier to understand a year or so down the road when a small change has to be made - especially if someone else has to make the change. So while there are more elegant ways of coding this, the code you have is about as clear an expression of your desired behavior as you will find.