rewrote these two functions with better self-caching