Better thread safety and exception handling