Improved the kgv() funtction