Some bug fixes and rearranging.