Added highly experimental and totally untested defensive logic, including a defensive state machine and some new constants and functions