Refactor notification handling code