working of sorts - needs cleanup