added console print and modularized with exception handling