Submitting solution per requirements. See comments within the code
for more details.