Refactor flash message code