several bugfixes, prepare to merge with Alex