Performance improvements. Getting rid of loops.