modularize and add documentation