Code reorg, refactoring and commenting.