lots of clean up and bug fixes