Rethought and refactoring in more logic way, tests added.