re-factor some code