made some code optimizations with references instead of copy by value