minor bug fixes and rearrangements