Fixed warnings and a bug in sorting