various bug and style fixes