Factorized some code