Added mutexes for thread safety
Reorder functions
Added Close() function

- fixes #3