Fully working code[n*log(n)]