Refactor tile caching into its own function