Fixed safeclose() bug filed by Chris