fix crashes and fix #6