code de-duplication, improved exception handling