modularize; add unit testing