Better event loop
Fix some memory leaks