added more test cases and made some code optimization