rearrange functions and add documentation