Small improvements 

Addition of defined( '_JEXEC' ) or die;
And 
Avoid full scan on users table for speedup in big tables