Restructure and clean up command execution