code refactored and ready for regression test