I've heard somewhere that Coffee improves mental function and i think it's true. Apart from improving mental function, coffee also have many other benefits.
Being a Doctor's Assistant, i'll say Melatonin is completely safe and can be taken to fall asleep but make you'll consult your Doctor before taking it...