Working code. Code refactoring required.