Some fine tuning and added unit tests