Bug fixes, more test cases