Some fine refactoring (and fixed a memory issue right along)