Added tests and fix issues

Added tests
Refactoring
Fix issues #2 #4 #5 #6 #7