refactored to fix pass-by-ref bug