Added sorting code to Logic