more tests + refactorings, including #165 and #166