Solved the problem with threads