finally got a working version of control calculation