Fixed resource leaks.
Fixed resource leaks and missing thread sync.
Fixed high CPU usage.