bug fixes for refactor; all cases tested successfully
Devishly cunning code cleanup