some refactoring and added basic caching