Remove duplicate code and clean up.