debugged and refined. Working now.