Humble beginnings of resource defragmentation

and proper API manipulation of resources.