Factorize some code