Improved performance drastically by making bkgrd thread use semaphore instead of while loop