Fix bugs; add main function for testing.