Refactor, move logic out of models.py, use worker threads