Clean up and start factoring out primitives