refactor normalize script for lower memory footprint