

Taken from sources of newt-0.51.6, but with many modifications, mainly bugfixes
and removal of some unneeded features.
