port bug fixes from vedis

This patch is based on fixes I made for vedis. Probably you should factor common code out to reduce duplication?

Fixes #3