Fixes several issues causing the script to malfunction