optimization
adding map, but it's still extremely slow
adding ssylvan's implementation

Copied from https://gist.github.com/ssylvan/5538011