Improve key test code