Refactored and cleaned codes. Refs #8878.