Clean up code and release references properly.