DRY refactoring

Remove some repetitions, I don't really like the name I chose for the new function, feel free to suggest something better ;).
There are no tests to verify if it is ok, but it looks quite straightforward. 

Cheers,
Luca