added some changes to emit statements to get output correct. Output for all 4 test cases is correct.