replaced rtrim with substr approach