Small code restructuring and bugfixes in test cases