Fix bugs found by dialyzer