code cleanup + some minor refactoring.