fix corrupted file caused by multiple Changing listeners
refactor trim trailing white spaces code into separate function