Implements parts of introsort, minor bugfixes