Attempt to factorize duplicate code