Improve caching and stack trace logic of getHandle
It should be fairly optimized