Some bug fixes. Still not entirely done.