implemented length mode, refactoring