Improved trail fix function.