Changes and optimizations

I wrote some changes and optimizations (also, a fix with setAttribute of an element).

Thanks.