Lots of tiny fixes, works again on win32