Bugfixes and small optimization for #6.