Extend calculation

Add new tests for border cases.
Extend calculation logic.