refactor some open/close calls and code around it