Pulled in refactored code into testing branch.

Pulled refactored code into testing branch.