refactored to improve memory footprint