remove some duplicated code
some more cleanup