Numerous bug fixes