ugly, but first working version of the script