Fixed some bugs. here you go, test logic, make sure output files are same.