Complete rewrite of most of the sourcecode