several bug fixes, prepare to merge with Alex