Improved hover detection code