Refactor of the commands code (removing duplicated code)