Bug fixes for memory management in functions.