Cleanup main loop and new error type

Error type that prints the operation in question for easier debug.
Resolve race condition

Make sure we allocate a new result object each time since the map
will keep pointing to the same memory space all the time even though
the map pointer itself might get copied.

thanks @axiom