Fix various bugs