Refactored some logic. Use async appendFile