Feature/segment caching

I'd like to get some feedback on this before merging into master.