slightly refactor base on paul's suggestion