bugfixes, cleanup, more precise testing