Bug fix and reordered loops