Refactor cache

A number of changes. One thing that I forgot to add but did:

* Make sure the cache always comes as first parameter. Should make it easier to understand what is going on in general due to consistency.