fixed total order issues, refactored code