implement part of convert() and fix several bugs