Numerous bug fixes, style tweaks