added sorting and fixed a buffer issue