added proper top distance detection to functions