Renamed some defines and added lots of security checks.