Fix sort and cleanup