cleaning up and streamlining code.

insure thread safety, clean up, and debugging.