How about something like this? It's missing tests and stuff but I hope it's in the right direction. Comments appreciated.<br>