Computing expected size; refactoring code into functions and procedures