refactor + save to file