Modularize and add unit tests.