Fix some of the many bugs