Fixes some bugs and introduces caching to some values