Cleaned up code, added some comments and a couple of example programs as their own file to facilitate testing.