speed improvements, cache at function level