For the sake of completion, implemented (what I believe) the correct solution