greatly improved memory management, though there's probably a better way to do it