Improve error handling, thread-safety