Improved key logic