wrote same function as provided in the book, took me a while to imagine it