Since you have two file areas could it be that in one case you are storing file in the wrong file area? I suggest to check the actual file table and see what you have there.
There is nothing obviously wrong in your code so the error is something small . I would recommend you to pass the code through style checker since there are quite a few stylistic errors.
Given that you already use github writing a travis script could be the easiest way for this.