script portion shortened and sligthly simplified. still doesn't do what i want