Started file rewriting logic

More changes coming next