function for 4 threads
merged