Some fixes and improvements.

I've fixed some bugs and misbehaviours. I think it's fine now, so you could send pull-request to main-branch.