Full refactoring.

I thank you for the wonderful script.
I improved it slightly and I think you'll like it.