working code for single input.