Refactored a bit and added more tests