Extract more functions from main