Refactored the codes. Refs #8601.