Some problems writing a threaded program