added sending mail code...
also fixed couple of bugs