wrote email function. check for bugs