refactoring to implement testing as in chapt 9