improved code structure

improved code structure for clarity