refactored and added tests and fixes
fixes & tests