adding flash messages and better error handling