add flash messages + refactor code