First draft. Seems to work