Fixed loops in gsUtils.js

Refactoring still has to come and also there were some loops I didn't
fix because they were using .splice() which shouldn't be used with
.forEach().

There are also a couple of unclear things that really need to be
refactored to be understandeable.