refactored code to logical format

modularized code now format is acceptable