programmatically finding maxD instead of hardcoding [WORKS]