add unit-tests and simplfy code for better testability
add unit-tests and simplfy code for better testability