improved invoice logic