Code refactoring

Indentation corrected, TAB characters omitted. To simplify the code some
scoped variables converted to functions parameters.