I think this method is responsible for doing too many things and should be split into smaller ones.