Refactor code, add command-line functions for development and testing.