Reimplemented 'latest_version()' to fix #12663 and make it more readable + better debug-able.