added logic for emails sending