Test cases and bugfixes