Some code tweaking and tests