Working code

Not tested
Submitted code