Fixed: memory leaks and errors