fix closing routine