Tidied up the script a bit, added new lines and generally made it more readable.