modularized and added unit tests