fix bug and some cleanup