Rearrange script and clean it up.