Cleanup and create an object

Just suggestions on how to make the lib more maintainable and understandable. I removed a lot of the commented out code, but if it's needed it's in the history. No new functionality added, but these changes should make it easier to expand. As a bonus the global variables are gone ;)