Refactor normalize function + bugfix.