after nearly 2 years I've rewritten this program a bit - it's not terrible