fix crash and some cleanup