optimizations + attempt to correct sorting