added some comments, put script in better order