Refactored and fixed cache, closes #9, thanks @goranata for reporting!