Refactor data structure and ctx