...and your problem is what exactly?? Your problem statement and code are quite clear, but you completely fail to mention which aspect is causing you problems &/or exactly the wisdom that you seek.
A user level that continues to overstate my experience :-))