Factorize code a bit