Removed duplicated code and fixed a problem with orders2invoice