refactor the code to have a close and open function