better error logging, and fix ref