Refactored to reduce Datastore queries.