some restructuring and better documentation