Agree the code isn't pretty. Note quite sure what you have in mind though. How would you improve this?