Separated IO and split into functions