Making the class thread-safe and other improvements

Check out the commit messages for more details :)