Full working code

Unit Tested working code with comments