Fix build errors due to returning references to local variables and temporaries