Cleaned up closest function to be more efficient.