Turned resource caching into a class, better header handling, tests.