Refactor some code to make it more generic.