refactored process function