Fix some horrible bugs