implemented Ensure class + general refactoring.