Fix legth calculation
Code cleanup