Changed some parameters to use const refrences instead of copies of variable for performance