Simplify script

Hopefully this also get's rid of the endless loops that happen sometime

Taken from http://stackoverflow.com/a/20288571