Cleaned up opening and closing of routines