using apply to pass all arguments to open and close functions