Working program now.  Needs some more testing, but basically works well