Fixed bug, added caching function