Remove duplication and split large methods apart.