Updated with faster shannon implementation from bjornedstrom