Fix various bugs

now it works.
What exactly was not working for you?