who wrote the song when i was your man