Split script and add documentation