Bug fixes, added sorting and various cosmetic changes