Clean up code, fix references