bug fix

avoid writing invalid locations