BUG Fix bugs caught by Tiago Queiroz