Clean up cwd handling in magic