Fixed some minor bugs, memory leaks