Added Tests and refactored