cleans up the code and, and fixes a few corner cases

There is a lot more to do, with error handling, and client data passing. Next to do is the initial server work.