extracted some code, tests passing