Since the first post I've managed to improve the code a bit. From the same link.
—
** Some more fixes againg. Update after one day since first post.