First, working version of deterministic, simple calculating algorithm