Fix main program. Add tests.