Implemented sample function and some caching