improves sorting and fixes template file