Refactor exec function