Fix various minor code flaws