re-factor some codes