Fixed thread manager and a bug in event loop