Improve code to make it closer to a polyfill