Working code -- to be cleaned