Rearranged result calculation, no changes
Various corrections and checks