Add log writing and previous functions