Cherry-picked bug fixes off 81376b5

As requested in https://github.com/DmitryBaranovskiy/raphael/pull/656

The bug fixes