Tests and some fixes