working code. not to submit as is