Refactor several function in a single one