Modified a bit of the looping structures for performance reasons
final performance improvements proved to be... minimal