Restructured the code and put in appropriate messages