Fix leftover bugs, all existing testcases now pass