Fixed some bugs found by Camporez