Split lib into more funcs for better testing.

Test more.