cmd utils fixes

fixes for all three of cmd_* utils, bunch of memleaks and segfaults.