everything but crucial comparator function and tidying up of code fixed