Numerous bug fixes, style changes