Using database trigger for some tasks. Refactor code