bugfixes ... tweaking script to make it more general