Fixed a bug and made the script a little more efficient