Bug fixes from tests and rearranging.