correct algorithm