code de-duplication and cleanup