Cleanup.

Changed for loops into ranged for loops, added checks for nullptr.

Signed-off-by: Lukasz Gromanowski <lgromanowski@gmail.com>