Implement historical mode and fix some ugly code