minor bug fixes. improve error handling and multi-threading synchronization. cleaned up compiler directives