Reduce code duplication.
Move element filtering back out of castRay().
Remove duplicate parameter.