re-organized script and made it more automagical