I couldn't figure out the solution on my own, source is commented in the first lines of the code