Merged Misho's and Jasen's solutions.