Fix callbacks to be thread-safe