Some improvements and added more tests.