Optimized datastore code. Made functionality common