Fixes Merged from Refactoring

General code refactoring. See differential for details.