refactor the security and database stuff to be more monadic