Fixed broken tests (+some bugs captured by tests)