added ordering and fixed minor bugs