fixed goodness function, split loop into two