dotProduct function speeds up code