Pretty much total rewrite. But thanks for the start :-)