clean and fix a few bugs