Refactoring, using gen.Task for blocking functions