Bug fix and minor code refactoring.
Fix circular dep.