4.2: implementing the actual algorithm, will probably not compile a lot.