Factorize some code.