Rearrange files and abstract the functions a little