Remove a lot of duplicate code.