refactoring and fixed conditional in open close method.