Rework, cleanup and improvements

Changes done based on experience with this library in real-world project.
It would probably be better to review this PR commit-by-commit due to files moving and reformatting. Reformatting can be verified by adding a `?w=1` key to GitHub commit URL.