added distance and used with pattern for opening file