Refactor cli code