Added more comments and checks to the script