Added cache support and refactor