fixed cache reste bug
improved memory safety
fixed bug with caching errors