Added safety checks. Add tests.