Works for one thread