Implemented both case 1 and case 2