Huge refactoring. Refs #3