modified to make sure tail new line and space to be removed