fix erroneous code path and added test cases to verify