Added more tests and improved the code a bit