Many code improvements, bugfixes