Main algorithm implementations.