Refactor to avoid casts