Many fixes and (hopefully) improvements