Fix various bugs

now it works.