Changed for-loops to for-each loops