Fix miscellaneous bug to make the script working

Now the script basically works without much problem.