memory leaks removed and kmean() improved