added some safety checks