Improved code and added a few more tests