Refactored solution to issue #239