Improved normalize function