Fix for issue #224

Also added some basic unit tests plus one threading test to ensure re-entrancy is possible