refactor and add flash message