Improve code testablity
Bunch of changes include refactoring for testabilitz