added some tests and refactored