replaced most foreach with raw for