Reorganise into separate library to allow testing.

Test program is built with library and google test.