Refactor trigger function