Finally got it more or less working but I don't have time test it properly. Still, it's a good start! PRs welcome :)