refactoring and much faster than given algorithm
unit tests
enc/dec unit tests passed