Refactored code after all updated and had some samll bugs and should enhance the code for better output