@bjori: Can you refactor this so we're not including external code? The algorithm seems simply enough.