reviewed ordering logic.. not optimized.. but works :)