Integrated Ajai and Sarath's code