Code cleanup and organization

Major cleanup in all code
error handling
locking
segfault fixes
three new methods added
more atomic writes (although they still require one unlock call, they don't' require search, get ptr, write, unlock)
comments throughout code
license headers added