fix for issue 241, part 1
fix for issue 241, part 2
code cleanup