Fixed caching. Incapsulated some repeated code.