Improved the shutdown mechanism. Server now waits until all threads are terminated before releasing. Fixed broken tests.