bug 1316: rework on calculation
bug 1316: Shorten the code even more and make it much more readable