Rewrite. Changed to a callback-based structure.