Rearrange functions and split them into separate files