I have refactored the code. And made necessary changes as suggested and required. Let me know if I can improve on some more issues. Thanks for reviewing.