Fixed memory leaks in main function and command functions.