Fixed some memory leaks; Moved many code snippets; Closes #4 and #5