fix bugs to support more complex test cases