added invert, and some minor cleanup