Fixed some mistakes and made coding a bit cleaner. Added some comments to script.