Updated code, with optimized principals, added argument vals and flags.