[WIP] Tests and various fixes