Refactor test script to use threading