Refactor and improve checking existence of a command