Added more testcases and fixed bugs revealed by them.