Fixing bugs reported by @spratt