hardening contract logic, added tests