Improved error handling for command excecution