Refactor in binary and library for easier testing.