fixed memleak, but there is a bug with the first pass instancefinding algorithm