Clean up and genericize code