Moved some global/static variables to the capture struct to make the initializaion more thread safe. It is improved, but still not perfect.