major cleanup and bug fixes