refactored safety code