Changed to use a single function for both threads