This was mostly what I implemented as utility library for another project. But I figured I can also extract it as it's useful on its own.