Add instances count test
Reformat code, fix issue #72, some sanity checks added