Various refactorings and cleanups.
Use a forEach method to get more readable loops