added intelligent open and close