Writing tests and minor tweaks