Refactored native code for multithreading