Use script.async=false when possible.
Also some refactoring.