removing unecessary trailing spaces and end tags