Fixed crashes, streamlined code