Thanks for the suggestions, @jakzal.
I added a new version with tests