Factorize duplicate code