Merging in new functionality.
Fixing minor variable scope issues.