Add test cases and minor script fixes