added more logic and tests