fix up for auto open feature and some code refactor