Added security checks, and some other fixes.