Proof of concept is fun!
Anyway, I thought I would post my code, before attempting to add Dridzt's modifications, just to see where I am at. The first pastebin is my entire code, complete with unnecessary, sometimes commented out parts. The second is pared down to include just the relevant stuff.
http://pastebin.com/A3w443DG -- full code. math breaks at line 476
http://pastebin.com/12RqSrk1 -- slimmed down. math breaks down at line 120