Code is working for small test cases