moved some calculations out of the kernel