fixed invalid mem accesses, working complete program