Improved the script, added some comments for better understanding, removed unnecessary lines.