Reordered functions; about to change a lot (especially memory management)