Some cleanup and passing by ref instead of val