bug fixes based on discussion with Andrew T.