Fixed some memory leaks and commands