or is there a better way of doing any part of it?
#7 below would be a start.
http://forum.arduino.cc/index.php/topic,148850.0.html