switch to pthread_cond_wait() instead of busy wait; added debug messages; other miscellanous changes